package com.diankangwo.factory.controller;


import cn.beecloud.bean.BCTransferParameter;
import com.diankangwo.common.annotation.SysLog;
import com.diankangwo.common.utils.PageUtils;
import com.diankangwo.common.utils.Query;
import com.diankangwo.common.utils.R;
import com.diankangwo.factory.dao.TdUserinfoMapper;
import com.diankangwo.factory.entity.TdUserinfo;
import com.diankangwo.factory.entity.TdWalletPayLog;
import com.diankangwo.factory.service.TdWalletPayLogService;
import com.diankangwo.util.BankUtil;
import com.diankangwo.util.BeecloudPayUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 钱包交易记录
 */
@RestController
@RequestMapping("/fac/walletpay")
public class TdWalletPayLogController {

    @Autowired
    private TdWalletPayLogService tdWalletPayLogService;

    @Autowired
    private TdUserinfoMapper tdUserinfoMapper;

    /**
     * 获取列表
     *
     * @param params
     * @return
     */
    @ApiOperation(value = "获取列表", notes = "")
    @ApiImplicitParam(name = "params", value = "查询条件", required = true, dataType = "Map<String,Object>")
    @SysLog("获取列表")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @RequiresPermissions("fac:walletpay:list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        //获取对应商品
        List<TdWalletPayLog> tdWalletPayLogs = tdWalletPayLogService.queryList(query);
        int total = tdWalletPayLogService.queryTotal(query);
        PageUtils pageUtil = new PageUtils(tdWalletPayLogs, total, query.getLimit(), query.getPage());
        return R.ok().put("page", pageUtil);
    }

    /**
     * 修改申请状态
     *
     * @param id
     * @param status
     * @return
     */
    @ApiOperation(value = "修改申请状态", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "status", value = "状态", required = true, dataType = "String")
    })
    @SysLog("修改端口设置")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @RequiresPermissions("fac:walletpay:update")
    public R update(@RequestParam Integer id, String status) {
        TdWalletPayLog tdWalletPayLog = tdWalletPayLogService.selectByPrimaryKey(id);
        TdUserinfo user = tdUserinfoMapper.selectByPrimaryKey(tdWalletPayLog.getUid());
        if (status.equals("拒绝")) {
            tdWalletPayLog.setPaystatus("拒绝提现");
            tdWalletPayLogService.updateByPrimaryKey(tdWalletPayLog);
        } else {
            Map<String, Object> optional = new HashMap<>();
            //调用提现流程
            optional.put("id", tdWalletPayLog.getId());
            optional.put("userId", user.getId());
            optional.put("totalFee", tdWalletPayLog.getTotalfee());
            optional.put("type", tdWalletPayLog.getUserpaystatus());
            //3.提现
            BeecloudPayUtil butil = new BeecloudPayUtil();
            BCTransferParameter bCTransferParameter = new BCTransferParameter();
            bCTransferParameter.setBillNo(tdWalletPayLog.getBillno());
            bCTransferParameter.setTotalFee((int) (tdWalletPayLog.getTotalfee() / 100));
            bCTransferParameter.setTitle("用户提现");
            bCTransferParameter.setTradeSource("OUT_PC");
            bCTransferParameter.setBankFullName(BankUtil.getNameOfBank(user.getBankcardno()));
            bCTransferParameter.setCardType("DE");
            bCTransferParameter.setAccountType("C");
            //测试时，请填写真实号码和姓名
            bCTransferParameter.setAccountNo(user.getBankcardno());
            bCTransferParameter.setAccountName(user.getName());
            bCTransferParameter.setMobile(user.getTel());
            bCTransferParameter.setOptional(optional);
            butil.startBCTransfer(bCTransferParameter);
        }
        return R.ok();
    }


}
